Load necessary libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
##
## The following object is masked from 'package:dplyr':
##
## select
library(terra)
## terra 1.7.18
##
## Attaching package: 'terra'
##
## The following object is masked from 'package:tidyr':
##
## extract
library(sf)
## Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(RColorBrewer)
library(tmap)
library(tmaptools)
Choose colors
# Set color palette
light_blue <- "#c7e0d8"
dark_blue <- "#083763"
# Read in AOI
aoi <- st_read("/capstone/kelpgeomod/new_file_structure/01-raw-data/New_AOI_SBchannel_shp/New_AOI_SBchannel.shp")
## Reading layer `New_AOI_SBchannel' from data source
## `/capstone/kelpgeomod/new_file_structure/01-raw-data/New_AOI_SBchannel_shp/New_AOI_SBchannel.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 1 field
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -120.65 ymin: 33.85 xmax: -118.8 ymax: 34.59
## Geodetic CRS: WGS 84
# Create a slightly larger bounding box
bbox <- st_bbox(c(xmin = -120.65 - 0.1,
xmax = -118.8 + 0.1,
ymax = 34.59 + 0.1,
ymin = 33.85 - 0.1))
# Define basemap options
basemap <- tm_basemap("Stamen.Terrain")
# Plot
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(aoi, bbox = bbox) +
basemap +
tm_borders(lwd = 2, col = dark_blue)
Quarter 1
# Read in kelp data and make sf object
kelp <- read_csv("/capstone/kelpgeomod/new_file_structure/03-analysis-data/maxent/quarter_1/kelp_presence_1.csv") %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
## Rows: 506 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): scientific_name
## dbl (3): longitude, latitude, quarter
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Read in the maxent output
maxent_output <- rast("/capstone/kelpgeomod/new_file_structure/03-analysis-data/maxent/quarter_1/results/Macrocystis_pyrifera_cloglog.tif")
# Read in the counties data
counties <- st_read("/capstone/kelpgeomod/new_file_structure/01-raw-data/land_bounds/California_County_Boundaries/cnty19_1.shp")
## Reading layer `cnty19_1' from data source
## `/capstone/kelpgeomod/new_file_structure/01-raw-data/land_bounds/California_County_Boundaries/cnty19_1.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 69 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -13849230 ymin: 3833650 xmax: -12704980 ymax: 5162403
## Projected CRS: WGS 84 / Pseudo-Mercator
# Set a palette
maxent_pal <- "BuGn"
# Plot results
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(maxent_output) +
tm_raster(title = "Predicted relative habitat suitability (0.0-1.0)", alpha = 0.9, palette = maxent_pal) + # Add the specified palette
tm_shape(counties) +
tm_polygons() +
# tm_shape(kelp) +
# tm_dots(col = dark_blue) +
tm_layout(
legend.position = c("right", "top")) + # Move the legend to the top-right corner
tm_layout(bg.color = "#E4FAFE") # Change the background color to blue
## legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.